//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.utils
{
	import flash.filters.GlowFilter;
	import flash.filters.BevelFilter;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	import flash.display.DisplayObject;
	
	/**
	 * A utility class for setting up filters.
	 * 
	 * @author Tony Birleffi
	 */
	public class FilterUtil 
	{
		/**
		 * Applies a glow to any display object.
		 * 
		 * @example
		 * <listing>
         * import org.as3commoncode.utils.FilterUtil;
         * FrameScriptUtil.applyGlow(displayObj, 0x000000, .5, 6, 6, 2, 1, false, false);
         * </listing>
         *
		 * @param displayObj	display object to apply the filter to.
		 * @param color			glow color, default is black.
		 * @param alpha			alpha value, default is 0.5.
		 * @param blurX			number, default is 6.
		 * @param blurY			number, default is 6.
		 * @param strength		number of strength, default is 2.
		 * @param quality		resolution, default is 1.
		 * @param inner			boolean, default is false.
		 * @param knockout		boolean, default is false.
		 */
		public static function applyGlow(displayObj:DisplayObject, color:Number = 0x000000, alpha:Number = 0.5, 
										  blurX:Number = 6, blurY:Number = 6, strength:Number = 2, quality:Number = 1, 
										  inner:Boolean = false, knockout:Boolean = false):void
		{
			// Define the glow filters array.
			var filter:GlowFilter = new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
			var glowFilters:Array = new Array();
			glowFilters.push(filter);
				
			// Apply the new glow filters array.
			displayObj.filters = glowFilters;
		}
		
		/**
		 * Applies a bevel to any display object.
		 * 
		 * @example
		 * <listing>
         * import org.as3commoncode.utils.FilterUtil;
         * FrameScriptUtil.applyBevel(displayObj, 4, 45, 16777215, 1, 0, 1, 4, 4, 1, 1, "inner", false);
         * </listing>
         *
		 * @param displayObj		display object to apply the filter to.
		 * @param distance			bevel distance, default is 4.
		 * @param angle				number angle of the bevel, default is 45.
		 * @param highlightColor	highlight color, default is 16777215.
		 * @param highlightAlpha	highlight alpha, default is 1.
		 * @param shadowColor		shadow color, default is 
		 * @param shadowAlpha		shadow alpha, default is 1.
		 * @param blurX				x blur distance, default is 4.
		 * @param blurY				y blur distance, default is 4.
		 * @param strength			number, default is 1.
		 */
		public static function applyBevel(displayObj:DisplayObject, distance:Number = 4, angle:Number = 45, highlightColor:uint = 16777215, 
										   highlightAlpha:Number = 1, shadowColor:uint = 0, shadowAlpha:Number = 1, blurX:Number = 4, blurY:Number = 4, 
										   strength:Number = 1, quality:int = 1, type:String = "inner", knockout:Boolean = false):void
		{
			// Define the bevel filters array.
			var filter:BevelFilter = new BevelFilter(distance, angle, highlightColor, highlightAlpha, shadowColor, shadowAlpha,
													  blurX, blurY, strength, quality, type, knockout);
			var filters:Array = new Array();
			filters.push(filter);
				
			// Apply the new bevel filters array.
			displayObj.filters = filters;
		}
		
		/**
		 * Applies a blur filter to any display object.
		 * 
		 * @example
		 * <listing>
		 * import org.as3commoncode.utils.FilterUtil;
		 * FrameScriptUtil.applyBlur(displayObj, 4, 4, 1);
		 * </listing>
		 * 
		 * @param displayObj	display object to apply the filter to.
		 * @param blurX			x blur distance.
		 * @param blurY			y blur distance.
		 * @param quality		1-3, resolution, default is 1.
		 */
		 public static function applyBlur(displayObj:DisplayObject, blurX:Number = 4, blurY:Number = 4, quality:int = 1):void
		 {
		 	// Define the blur filters array.
			var filter:BlurFilter = new BlurFilter(blurX, blurY, quality);
			var filters:Array = new Array();
			filters.push(filter);
				
			// Apply the new blur filters array.
			displayObj.filters = filters;
		 }
		 
		 /**
		  * Applies a drop shadow to any display object.
		  * 
		  * @example
		  * <listing>
		  * import org.as3commoncode.utils.FilterUtil;
		  * FrameScriptUtil.applyDropShadow(displayObj, 4, 45, 0, 1, 4, 4, 1, 1, false, false, false);
		  * </listing>
		  * 
		  * @param displayObj	display object to apply the filter to.
		  * @param distance		number, distance of the drop shadow, default is 4.
		  * @param angle		number, default is 45.
		  * @param color		uint, default is black.
		  * @param alpha		number, default is 1.
		  * @param blurX		x blur distance, default is 4.
		  * @param blurY		y blur distance, default is 4.
		  * @param quality		quality, default is 1.
		  * @param inner		boolean, default is false.
		  * @param knockout		boolean, default is false.
		  * @param hideObject	boolean, default is false.
		  */
		 public static function applyDropShadow(displayObj:DisplayObject, distance:Number = 4, angle:Number = 45, color:uint = 0,
		 										 alpha:Number = 1, blurX:Number = 4, blurY:Number = 4, strength:Number = 1, 
		 										 quality:int = 1, inner:Boolean = false, knockout:Boolean = false, hideObject:Boolean = false):void
		 {
		 	// Define the drop shadow filters array.
			var filter:DropShadowFilter = new DropShadowFilter(distance, angle, color, alpha, blurX, blurY, 
																strength, quality, inner, knockout, hideObject);
			
			var filters:Array = new Array();
			filters.push(filter);
				
			// Apply the new drop shadow filters array.
			displayObj.filters = filters;
		 }
	}
}